package com.test.deencredit.activity_deencredit.ln_deencredit;

import static com.test.deencredit.config_deencredit.UserState_deencredit.*;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentTransaction;

import com.test.deencredit.R;
import com.test.deencredit.base_deencredit.BaseActivity_deencredit;
import com.test.deencredit.base_deencredit.BaseLnFragment_deencredit;
import com.test.deencredit.bean_deencredit.process_deencredit.ProcessInfo_deencredit;
import com.test.deencredit.config_deencredit.UserState_deencredit;
import com.test.deencredit.dialog_deencredit.CustomerServiceDialog_deencredit;
import com.test.deencredit.fragment_deencredit.ln_state_deencredit.LnRpyFragment_deencredit;
import com.test.deencredit.fragment_deencredit.ln_state_deencredit.LnStateFailFragment_deencredit;
import com.test.deencredit.fragment_deencredit.ln_state_deencredit.LnStateWaitFragment_deencredit;
import com.test.deencredit.fragment_deencredit.main_deencredit.HomeFragment_deencredit;
import com.test.deencredit.fragment_deencredit.main_deencredit.PersonalCenterFragment_deencredit;
import com.test.deencredit.util_deencredit.http_deencredit.HttpUtil_deencredit;

import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity_deencredit extends BaseActivity_deencredit {

    private View homeView_deencredit, pcView_deencredit;
    private ImageView homeIv_deencredit, pcIv_deencredit;
    private TextView homeTv_deencredit, pcTv_deencredit;

    private View csView_deencredit;
    private ImageView csIv_deencredit;
    private TextView csTv_deencredit;

    private BaseLnFragment_deencredit baseLnFragment_deencredit;
    private PersonalCenterFragment_deencredit pcFragment_deencredit;

    private int pageFlag_deencredit = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_deencredit);

        page_deencredit = "MAIN";

        homeView_deencredit = findViewById(R.id.am_view_home);
        pcView_deencredit = findViewById(R.id.am_view_me);
        homeIv_deencredit = findViewById(R.id.am_iv_home);
        pcIv_deencredit = findViewById(R.id.am_iv_me);
        homeTv_deencredit = findViewById(R.id.am_tv_home);
        pcTv_deencredit = findViewById(R.id.am_tv_me);

        csView_deencredit = findViewById(R.id.am_view_cs);
        csIv_deencredit = findViewById(R.id.am_iv_cs);
        csTv_deencredit = findViewById(R.id.am_tv_cs);

        init_deencredit();
    }

    private void init_deencredit(){

        pcFragment_deencredit = new PersonalCenterFragment_deencredit();

        homeView_deencredit.setOnClickListener(v -> toHome_deencredit());
        pcView_deencredit.setOnClickListener(v -> toPc_deencredit());
        csView_deencredit.setOnClickListener(v -> {
            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"CUSTOMER_SERVICE_CLICK","");
            showCs_deencredit();
            new CustomerServiceDialog_deencredit(this).show_deencredit(this::dismissCs_deencredit);
        });

        //初始化用户状态
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.am_layout_fragment, pcFragment_deencredit).commitAllowingStateLoss();
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateUserState_deencredit();
    }

    /**
     * 判断用户流程状态，如果状态有改变再更新页面
     */
    private void updateUserState_deencredit(){
        //判断用户流程状态
        String state = ProcessInfo_deencredit.getInstance_deencredit().getUserState_deencredit();
        if (UserState_deencredit.isStateChange_deencredit(state)) updatePageState_deencredit(state);
    }

    private void updatePageState_deencredit(String userState) {

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

        if (baseLnFragment_deencredit != null) fragmentTransaction.remove(baseLnFragment_deencredit);

        switch (userState){
            case State_Rp_deencredit:
                baseLnFragment_deencredit = new LnRpyFragment_deencredit();
                break;
            case State_Wait_deencredit:
                baseLnFragment_deencredit = new LnStateWaitFragment_deencredit();
                break;
            case State_Fail_deencredit:
                baseLnFragment_deencredit = new LnStateFailFragment_deencredit();
                break;
            default:
                baseLnFragment_deencredit = new HomeFragment_deencredit();
        }

        fragmentTransaction.add(R.id.am_layout_fragment, baseLnFragment_deencredit);

        switch (pageFlag_deencredit) {
            case 2:
                fragmentTransaction.show(pcFragment_deencredit).hide(baseLnFragment_deencredit);
                break;
            default:
                fragmentTransaction.show(baseLnFragment_deencredit).hide(pcFragment_deencredit);
        }

        fragmentTransaction.commitAllowingStateLoss();
    }

    private void toHome_deencredit(){

        HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"HOME_CLICK","");

        pageFlag_deencredit = 1;

        homeIv_deencredit.setBackgroundResource(R.drawable.icon_home_true_deencredit);
        pcIv_deencredit.setBackgroundResource(R.drawable.icon_pc_false_deencredit);
        csIv_deencredit.setBackgroundResource(R.drawable.icon_cs_false_deencredit);

        homeTv_deencredit.setTextColor(getColor(R.color.tab_text_true_deencredit));
        pcTv_deencredit.setTextColor(getColor(R.color.tab_text_false_deencredit));
        csTv_deencredit.setTextColor(getColor(R.color.tab_text_false_deencredit));

        getSupportFragmentManager().beginTransaction().show(baseLnFragment_deencredit).hide(pcFragment_deencredit).commitAllowingStateLoss();

    }

    private void toPc_deencredit(){

        HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"ME_CLICK","");

        pageFlag_deencredit = 2;

        homeIv_deencredit.setBackgroundResource(R.drawable.icon_home_false_deencredit);
        pcIv_deencredit.setBackgroundResource(R.drawable.icon_pc_true_deencredit);
        csIv_deencredit.setBackgroundResource(R.drawable.icon_cs_false_deencredit);

        homeTv_deencredit.setTextColor(getColor(R.color.tab_text_false_deencredit));
        pcTv_deencredit.setTextColor(getColor(R.color.tab_text_true_deencredit));
        csTv_deencredit.setTextColor(getColor(R.color.tab_text_false_deencredit));

        getSupportFragmentManager().beginTransaction().show(pcFragment_deencredit).hide(baseLnFragment_deencredit).commitAllowingStateLoss();

    }

    private void showCs_deencredit(){

        HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"CUSTOMER_SERVICE_CLICK","");

        homeIv_deencredit.setBackgroundResource(R.drawable.icon_home_false_deencredit);
        pcIv_deencredit.setBackgroundResource(R.drawable.icon_pc_false_deencredit);
        csIv_deencredit.setBackgroundResource(R.drawable.icon_cs_true_deencredit);

        homeTv_deencredit.setTextColor(getColor(R.color.tab_text_false_deencredit));
        pcTv_deencredit.setTextColor(getColor(R.color.tab_text_false_deencredit));
        csTv_deencredit.setTextColor(getColor(R.color.tab_text_true_deencredit));

    }

    private void dismissCs_deencredit(){
        if (pageFlag_deencredit == 2){
            homeIv_deencredit.setBackgroundResource(R.drawable.icon_home_false_deencredit);
            pcIv_deencredit.setBackgroundResource(R.drawable.icon_pc_true_deencredit);
            csIv_deencredit.setBackgroundResource(R.drawable.icon_cs_false_deencredit);

            homeTv_deencredit.setTextColor(getColor(R.color.tab_text_false_deencredit));
            pcTv_deencredit.setTextColor(getColor(R.color.tab_text_true_deencredit));
            csTv_deencredit.setTextColor(getColor(R.color.tab_text_false_deencredit));
        }else {
            homeIv_deencredit.setBackgroundResource(R.drawable.icon_home_true_deencredit);
            pcIv_deencredit.setBackgroundResource(R.drawable.icon_pc_false_deencredit);
            csIv_deencredit.setBackgroundResource(R.drawable.icon_cs_false_deencredit);

            homeTv_deencredit.setTextColor(getColor(R.color.tab_text_true_deencredit));
            pcTv_deencredit.setTextColor(getColor(R.color.tab_text_false_deencredit));
            csTv_deencredit.setTextColor(getColor(R.color.tab_text_false_deencredit));
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UserState_deencredit.clearState_deencredit();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (baseLnFragment_deencredit !=null && baseLnFragment_deencredit instanceof HomeFragment_deencredit) EasyPermissions.onRequestPermissionsResult(requestCode,permissions,grantResults, baseLnFragment_deencredit);
    }

}